
package botlab.gui.levelchooser.actionlistener;

import botlab.Game;
import botlab.GameManager;
import botlab.gui.GUI;
import botlab.gui.Notifier;
import botlab.gui.levelchooser.LevelChooserPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Quentin
 */
public class SelectActionListener implements ActionListener
{
    private GUI gui;
    private LevelChooserPanel panel;
    
    public SelectActionListener(LevelChooserPanel panel,GUI gui)
    {
        this.panel=panel;
        this.gui=gui;
    }

    public void actionPerformed(ActionEvent e)
    {
        final Game game = GameManager.getInstance().getGame();
        final ActionListener callBack = this;
        
        final botlab.level.Level l = panel.getLevels().get(panel.getSlide().getImageName());
        
        game.enqueue(new Callable<Void>()
        {
            public Void call()
            {
                try
                {
                    game.cleanup();
                    game.loadLevel(l,callBack);
                } 
                catch (Exception ex)
                {
                    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE,
                            null, ex);
                }
                return null;
            }
        });
        panel.getSlide().setIcon(null);
        panel.getSlide().setText("Loading...");
    }
    
    public void levelLoaded()
    {
        gui.setMode(GUI.EDITOR);
        Notifier.getInstance().enterState("editor");
        panel.getSlide().reset();
        gui.getHelpFrame().levelSelected();
    }
    
}
